/*
 * Created on 2005-3-30
 *
 */
package com.hd123.rumba.impex;

import java.util.Date;

import com.hd123.rumba.cartridge.FVRumbaSystem;
import com.hd123.rumba.exception.StringTooLongException;
import com.hd123.rumba.runtime.VersionedPersistent;
import com.hd123.rumba.util.StringUtil;

/**
 * @author caili
 */
public class ImpexLog extends VersionedPersistent {

  /** 构件包前缀 */
  public static final String FUNCV_PREFIX = FVRumbaSystem.FUNCV_PREFIX;

  public static final String EXPORTING = "导出中";
  public static final String ABORTED = "取消";
  public static final String COMMITED = "完成";
  public static final String REMOTE = "<remote>";

  private Indicator indicator; // belongs to which indicator

  private ExportCondition currentExportCondition;
  public static final String CAPTION_CURCOND_TIME = "当前时间条件";
  public static final String CAPTION_CURCOND_ID = "当前ID条件";

  private ExportCondition newExportCondition;
  public static final String CAPTION_NEWCOND_TIME = "新的时间条件";
  public static final String CAPTION_NEWCOND_ID = "新的ID条件";

  private String state;
  public static final String CAPTION_STATE = "状态";

  private Date startTime;
  public static final String CAPTION_STARTTIME = "开始时间";

  private Date endTime = null;
  public static final String CAPTION_ENDTIME = "结束时间";

  private Date commitTime = null;
  public static final String CAPTION_COMMITTIME = "完成时间";

  private String userFullLogin;
  public static final String CAPTION_USERFULLLOGIN = "用户";
  public static final int LENGTH_userFullLogin = 50;

  private String description;
  public static final String CAPTION_DESCRIPTION = "描述";
  public static final int LENGTH_description = 1024;

  private String fileName; // 导入/导出文件名. 远程调用使用REMOTE;
  public static final String CAPTION_FILENAME = "文件名";
  public static final int LENGTH_fileName = 1024;

  private int total = 0;
  public static final String CAPTION_TOTAL = "ttl";

  private int inserted = 0;
  public static final String CAPTION_INSERTED = "ins";

  private int updated = 0;
  public static final String CAPTION_UPDATED = "upd";

  private int deleted = 0;
  private int success = 0;
  public static final String CAPTION_SUCCESS = "succ";

  private int fail = 0;
  public static final String CAPTION_FAIL = "fail";

  private int size = 0; // 要求导出的笔数. 0=全部
  public static final String CAPTION_SIZE = "size";

  private int available; // 总共要导出的笔数.
  public static final String CAPTION_AVAILABLE = "avail";

  public static final String PERMISSION_DELETE = "导入导出日志.删除权";/* "导入导出日志.删除"; */
  public static final String PERMISSION_VIEW = "导入导出.查看权";
  // 以下属性不要持久化
  private StringBuffer messages = new StringBuffer();

  public ImpexLog addMessage(int position, String message) {
    messages.append("第" + position + "笔: " + message);
    return this;
  }

  public ImpexLog addMessage(String message) {
    messages.append(message);
    return this;
  }

  public String getMessage(String style) {
    StringBuffer sb = new StringBuffer();
    sb.append(fileName + ": ");
    if (messages.length() > 0)
      sb.append(messages);
    sb.append(" ");
    if (style.equals(Indicator.IMPORT)) {
      if (total > 0) {
        sb.append("共处理" + total + "笔. ");
        sb.append("添加" + inserted + "笔, ");
        sb.append("更新" + updated + "笔. ");
      }
    } else if (style.equals(Indicator.EXPORT)) {
      sb.append("共导出" + total + "笔. ");
    }
    return sb.toString();
  }

  public void incSuccess() {
    success++;
  }

  public void incFail() {
    fail++;
  }

  public void incInserted() {
    inserted++;
  }

  public void incDeleted() {
    deleted++;
  }

  public void incUpdated() {
    updated++;
  }

  // 以下是getters/setters

  public int getDeleted() {
    return deleted;
  }

  public void setDeleted(int deleted) {
    this.deleted = deleted;
  }

  public int getFail() {
    return fail;
  }

  public void setFail(int fail) {
    this.fail = fail;
  }

  public int getInserted() {
    return inserted;
  }

  public void setInserted(int inserted) {
    this.inserted = inserted;
  }

  public int getSuccess() {
    return success;
  }

  public void setSuccess(int success) {
    this.success = success;
  }

  public int getTotal() {
    return total;
  }

  public void setTotal(int total) {
    this.total = total;
  }

  public int getUpdated() {
    return updated;
  }

  public void setUpdated(int updated) {
    this.updated = updated;
  }

  public Date getCommitTime() {
    return commitTime;
  }

  public void setCommitTime(Date commitTime) {
    this.commitTime = commitTime;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    try {
      if (description != null)
        description = StringUtil.truncate(description, LENGTH_userFullLogin, true);
    } catch (StringTooLongException e) {
      ;
    }
    this.description = description;
  }

  public Date getEndTime() {
    return endTime;
  }

  public void setEndTime(Date endTime) {
    this.endTime = endTime;
  }

  public Indicator getIndicator() {
    return indicator;
  }

  public void setIndicator(Indicator indicator) {
    this.indicator = indicator;
  }

  public Date getStartTime() {
    return startTime;
  }

  public void setStartTime(Date startTime) {
    this.startTime = startTime;
  }

  public String getState() {
    return state;
  }

  public void setState(String state) {
    this.state = state;
  }

  public ExportCondition getCurrentExportCondition() {
    return currentExportCondition;
  }

  public void setCurrentExportCondition(ExportCondition currentExportCondition) {
    this.currentExportCondition = currentExportCondition;
  }

  public ExportCondition getNewExportCondition() {
    return newExportCondition;
  }

  public void setNewExportCondition(ExportCondition newExportCondition) {
    this.newExportCondition = newExportCondition;
  }

  public int getSize() {
    return size;
  }

  public void setSize(int size) {
    this.size = size;
  }

  public String getUserFullLogin() {
    return userFullLogin;
  }

  public void setUserFullLogin(String userFullLogin) {
    try {
      if (userFullLogin != null)
        userFullLogin = StringUtil.truncate(userFullLogin, LENGTH_userFullLogin, true);
    } catch (StringTooLongException e) {
      ;
    }
    this.userFullLogin = userFullLogin;
  }

  public String getFileName() {
    return fileName;
  }

  public void setFileName(String fileName) {
    try {
      if (fileName != null)
        fileName = StringUtil.truncate(fileName, LENGTH_userFullLogin, true);
    } catch (StringTooLongException e) {
      ;
    }
    this.fileName = fileName;
  }

  public int getAvailable() {
    return available;
  }

  public void setAvailable(int available) {
    this.available = available;
  }

  public StringBuffer getMessages() {
    return messages;
  }

  public void setMessages(StringBuffer messages) {
    this.messages = messages;
  }
}
